<?php

namespace app\common\model\mysql;

use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\Exception;
use think\db\exception\ModelNotFoundException;
use think\Model;

class BaseModel extends Model
{

    /**
     * 自动生成写入时间
     * @var bool
     */
    protected $autoWriteTimestamp = true;

    /**
     * 通过条件查询列表
     * @param $where
     * @param $page
     * @param $limit
     * @param $field
     * @return BaseModel|array|false|mixed|Model|null
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getNormalListByWhere($where,$field='*'){
        $list = $this->where($where)->field($field)->order('id desc')->select();
        return $list;
    }

    /**
     * @param $where
     * @param $order
     * @param string $field
     * @return BaseModel[]|array|\think\Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getNormalListByWhereWithOrder($where,$order,$field='*'){
        $list = $this->where($where)->field($field)->order($order)->select();
        return $list;
    }

    /**
     * @param $where
     * @param $limit
     * @param $field
     * @return \think\Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getNormalListByLimit($where,$limit,$order,$field='*'){
        $list = $this->where($where)->limit($limit)->field($field)->order($order)->select();
        return $list;
    }

    /**
     * 通过条件及分页查询列表
     * @param $where
     * @param $page
     * @param $limit
     * @param $field
     * @return BaseModel[]|array|\think\Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getPageListByWhere($where,$page,$limit,$field='*'){
        $list = $this->where($where)->page($page,$limit)->field($field)->order('id desc')->select();
        return $list;
    }

    /**
     * @param $where
     * @param $page
     * @param $limit
     * @param $order
     * @param $field
     * @return BaseModel[]|array|\think\Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getPageListByWhereWithOrder($where,$page,$limit,$order,$field='*'){
        $list = $this->where($where)->page($page,$limit)->field($field)->order($order)->select();
        return $list;
    }

    /**
     * 根据指定字段获取信息，必须为字符串字段
     * @param $field
     * @param $value
     * @param $selectField
     * @return BaseModel|array|false|mixed|Model|null
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getNormalByField($field,$value,$selectField='*'){
        if (empty($value)) {
            return false;
        }

        $where = [
            $field => trim($value),
        ];

        return $this->field($selectField)->where($where)->find();
    }

    /**
     * @param $join
     * @param $field
     * @param $value
     * @param $selectField
     * @return BaseModel|array|false|mixed|Model|null
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getJoinNormalByField($join,$field,$value,$selectField='*'){
        if (empty($value)) {
            return false;
        }

        $where = [
            $field => trim($value),
        ];

        return $this::with($join)->field($selectField)->where($where)->find();
    }

    /**
     * @param $where
     * @param string $selectField
     * @return array|Model|null
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getNormalByWhere($where,$selectField='*'){
        return $this->field($selectField)->where($where)->find();
    }

    /**
     * 通过id获取信息
     * @param $id
     * @param $field
     * @return BaseModel|array|false|mixed|Model|null
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getById($id,$field='*')
    {
        //此处如果确定知道参数为空则提前返回false，避免带着错误条件查询数据库浪费i/o资源
        if (empty($id)) {
            return false;
        }

        $where = [
            "id" => trim($id),
        ];
        return $this->field($field)->where($where)->find();
    }

    /**
     * @param $id
     * @param $data
     * @return bool
     */
    public function updateById($id, $data): bool
    {
        $id = intval($id);
        if (empty($id) || empty($data) || !is_array($data)) {
            return false;
        }

        $data['update_time'] = time();

        $where = [
            "id" => $id
        ];
        return $this->where($where)->save($data);
    }

    /**
     * @param $ids
     * @param $data
     * @return bool
     */
    public function updateByIds($ids, $data): bool
    {
        if (empty($ids) || empty($data) || !is_array($data)) {
            return false;
        }

        $data['update_time'] = time();

        $where = [
            ['id','in',$ids]
        ];
        return $this->where($where)->save($data);
    }

    /**
     * 新增/更新数据
     * @param $data
     * @return bool
     * @throws Exception
     */
    public function saveData($data): bool
    {
        $db=$this;
        if(isset($data['id'])){
            $db=$this->find($data['id']);
            if(!$db){
                throw new Exception('数据不存在');
            }
        }
        return $db->save($data);
    }

    /**
     * 根据id删除
     * @param $id
     * @return bool
     */
    public function deleteById($id): bool
    {
        return $this->where(['id'=>$id])->delete();
    }

    /**
     * 根据id修改状态
     * @param $data
     * @return bool
     * @throws DataNotFoundException
     * @throws DbException
     * @throws Exception
     * @throws ModelNotFoundException
     */
    public function updateStatus($data): bool
    {
        $row=$this->find($data['id']);
        if(!$row){
            throw new Exception('数据不存在');
        }
        return $row->save(['status'=>$data['status']]);
    }

    /**
     * 获取分页列表
     * @param $where
     * @param $page
     * @param $limit
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getTableList($where,$page,$limit): array
    {
        if(!empty($page)&&!empty($limit)){
            $list=$this->where($where)->order('id desc')->page($page,$limit)->select();
        }else{
            $list=$this->where($where)->order('id desc')->select();
        }
        $list=$list->isEmpty()?[]:$list->toArray();
        $count=$this->where($where)->count();
        return ['count'=>$count,'list'=>$list];
    }
}
